fix(features): lazy package imports so one broken module can't sink the rest (#10)#35
Open
bradsmithmba wants to merge 1 commit into
Open
Conversation
…he rest src/features/__init__.py eagerly imported FeatureEngineering (from base.py) and the regime feature classes. base.py imports the data layer, so the single broken import took down the entire package: importing the self-contained modules (greeks, pnl, position_models, position_state, position_vector) failed even though none of them depend on base or data. Switch __init__ to PEP 562 lazy attribute loading. The public API is unchanged (`from src.features import FeatureEngineering` still works, loading base on first access), but importing the package or any clean submodule no longer triggers base's dependency chain. Verified: `import src.features.greeks` now succeeds with the data layer still absent; base is not loaded until FeatureEngineering/RegimeStateVector is actually accessed. Adds regression tests. This is complementary to cloudtrainerwork#1 (which fixes the missing data layer at the root); it contains the blast radius regardless. Closes cloudtrainerwork#10 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
src/features/__init__.pyeagerly importedFeatureEngineering(frombase.py) and the regime feature classes (fromregime_features.py) at package import time.base.pyimports the data layer (from ..data import get_default_provider, CacheManager, MarketDataRequest), so that one broken import took down the entire package: importing the self-contained modules —greeks,pnl,position_models,position_state,position_vector— failed even though none of them depend onbaseordata.Closes #10.
Dependency map
baseregime_featuresbase)greeks,pnl,position_models,position_state,position_vectorUnder the eager
__init__, the five clean modules were unreachable because of the two that aren't.Fix
Switch
__init__to PEP 562 lazy attribute loading (__getattr__). The public API is unchanged —from src.features import FeatureEngineeringstill works, importingbaseon first access — but importing the package or any clean submodule no longer triggersbase's dependency chain.Verification (with the data layer still absent, i.e. before #1)
Adds
tests/features/test_lazy_init.py(3 tests) pinning the laziness.Relationship to #1
Complementary, not overlapping. #1 (PR #19) fixes the missing data layer at the root; this PR contains the blast radius so a broken module in
src.featurescan't sink unrelated ones. They touch different files and merge independently.🤖 Generated with Claude Code